package com.lyfx.ly.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lyfx.ly.entity.AttrEntity;
import com.lyfx.ly.service.AttrAttrgroupService;
import com.lyfx.ly.service.AttrService;
import com.lyfx.ly.service.ClassCategoryService;
import com.lyfx.ly.vo.AttrGroupRelationVo;
import com.lyfx.ly.vo.AttrGroupWithAttrsVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyfx.ly.entity.AttrGroupEntity;
import com.lyfx.ly.service.AttrGroupService;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.R;


/**
 * @author lsy
 * @email lsy@gmail.com
 * @date 2020-07-11 17:57:41
 */
@RestController
@RequestMapping("ly/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private ClassCategoryService classCategoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupService relationService;



    /**
     * ly/attrgroup/8/withattr
     * 获取所有课程下的属性分组以及所有属性
     */
    @GetMapping("/{categoryId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("categoryId")Long categoryId){
        //1、查出当前分类下的所有属性分组
        //2、查出每个分组下的所有属性
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrsByCategoryId(categoryId);
        return R.ok().put("data", data);
    }

   //ly/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos ){
        relationService.saveBatch(vos);
        return R.ok();
    }

    ///ly/attrgroup/3/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entity = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entity);
    }
    ///ly/attrgroup/1/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page =  attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", page);
    }
    //ly/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }



    /**
     * 列表
     */
    @RequestMapping("/list/{catlogId}")
    // @RequiresPermissions("ly:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catlogId") Long catlogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catlogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("ly:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long categoryId = attrGroup.getCategoryId();
        Long[] path = classCategoryService.findCategoryPath(categoryId);
        attrGroup.setCategoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("ly:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("ly:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("ly:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
